/*
 * Copyright (c) 2009-2013 EvTech Project
 * All rights reserved.
 * 
 * This file is part of LModeller.
 *
 * LModeller is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 *
 * LModeller is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LModeller.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package fi.honkalampisaatio.lmodeller.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EnumSet;

import fi.honkalampisaatio.lmodeller.LanguageResource;
import fi.honkalampisaatio.lmodeller.LModeller;
import fi.honkalampisaatio.lmodeller.Logger;
import fi.honkalampisaatio.lmodeller.selection.*;
import fi.honkalampisaatio.lmodeller.ui.GameButton.TEXTLOCATION;
import fi.honkalampisaatio.lmodeller.xml.User;
import fi.honkalampisaatio.lwjgl.GLTexture;
import glapp.GLApp;

import org.lwjgl.util.Rectangle;

public class GameMenu extends GameView implements ActionListener {
	GameButton setup;


	public GameMenu() {
		super(EnumSet.of(BUTTONFLAGS.BACK, BUTTONFLAGS.UPLABEL, BUTTONFLAGS.VOICE));
	}
	
	
    public void init()
    {
    	int h = GLApp.getHeight()/10;
    	int w = GLApp.getWidth()/10;
    	int minFontSize, tmpFontSize;

    	setup = new GameButton(new Rectangle((int)(w*0.1),(int)(h*0.1),(int)(w*1.8), (int)(h*0.8)), 
    			GameTextures.getInstance().getTexture("SETUP_BUTTON")
    			, LModeller.actionColor, LanguageResource.getInstance().get("SETTINGS"), TEXTLOCATION.RIGHT);
    	GameButton memoryGame = new GameButton(new Rectangle((int)(w*6),3*h,w*3,h*4), GameTextures.getInstance().getTexture("MEMORYGAME_BUTTON")
    			, LModeller.actionColor, null, TEXTLOCATION.UP);
    	GameTextLabel memoryGameLabel = new GameTextLabel(new Rectangle((int)(w*6),h*7,w*3,h),  
    			(int)(h*0.8), LModeller.actionColor);
    	tmpFontSize = memoryGameLabel.setText(LanguageResource.getInstance().get("MEMORY_GAME"));
    	minFontSize = tmpFontSize;
    	GameButton buildGame = new GameButton(new Rectangle(w,3*h,w*3,h*4), GameTextures.getInstance().getTexture("BUILDMENU_BUTTON")
    			, LModeller.actionColor, null, TEXTLOCATION.UP);
    	GameTextLabel buildGameLabel = new GameTextLabel(new Rectangle(w,h*7,w*3,h), 
    			(int)(h*0.8), LModeller.actionColor);
    	tmpFontSize = buildGameLabel.setText(LanguageResource.getInstance().get("BUILD_GAME"));
    	if(tmpFontSize<minFontSize && tmpFontSize>0) {
        	minFontSize = tmpFontSize;
    	}
    	super.addComponent(setup);
    	super.addComponent(memoryGame);
    	super.addComponent(memoryGameLabel);
    	super.addComponent(buildGame);
    	super.addComponent(buildGameLabel);

    	memoryGame.setToolTipSentence(LanguageResource.getInstance().get("MEMORY_GAME"));
    	buildGame.setToolTipSentence(LanguageResource.getInstance().get("BUILD_GAME"));
    	setup.setToolTipSentence(LanguageResource.getInstance().get("SETTINGS"));
    	setInstructionSentence(LanguageResource.getInstance().get("GAMEMENU_INSTRUCTION"));

    	memoryGame.addActionListener(this);
    	memoryGame.setActionCommand("MEMORY_GAME");
    	buildGame.addActionListener(this);
    	buildGame.setActionCommand("BUILD_GAME");
    	setup.addActionListener(this);
    	setup.setActionCommand("SETTINGS");

    	memoryGameLabel.setMaxFontSize(minFontSize);
    	buildGameLabel.setMaxFontSize(minFontSize);
    }
    

	public void setUser(User user, GLTexture userTexture) {
		if(user!=null)
		{
			setUpLabelText(user.getName());
			setup.setVisible(user.getShowSettings());
		}
		else
		{
			setUpLabelText(LanguageResource.getInstance().get("CHOOSE_GAME"));
			setup.setVisible(false);
		}
    	setUpToolTipSentence(getUpLabelText());
    	setUpLabelTexture(userTexture);
	}


	@Override
	protected void refreshPage(int page) {
	}



	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() !=null && e.getActionCommand().equalsIgnoreCase("SETTINGS"))
		{
			Logger.getInstance().log("setup selected");
			GameModeSelectionEvent se = new GameModeSelectionEvent(this, GameModeSelectionEvent.MODES.SETUP);
			this.fireSelectionEvent(se);
		}
		if(e.getSource() !=null && e.getActionCommand().equalsIgnoreCase("MEMORY_GAME"))
		{
			Logger.getInstance().log("Memory Game selected");
			GameModeSelectionEvent se = new GameModeSelectionEvent(this, GameModeSelectionEvent.MODES.MEMORY_GAME);
			this.fireSelectionEvent(se);
		}
		if(e.getSource() !=null && e.getActionCommand().equalsIgnoreCase("BUILD_GAME"))
		{
			Logger.getInstance().log("Build Game selected");
			GameModeSelectionEvent se = new GameModeSelectionEvent(this, GameModeSelectionEvent.MODES.BUILD_MENU);
			this.fireSelectionEvent(se);
		}
	}

}
